package com.ai.sml.service.impl;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.Protocol;

import com.ai.frame.bean.OutputObject;
import com.ai.frame.util.JsonUtil;
import com.ai.sml.service.ICacheService;
import com.ai.sml.util.JedisSingleUtil;
import com.ai.sml.util.MyOwnRuntimeException;
import com.ai.sml.util.StringUtil;

@SuppressWarnings("serial")
public class CacheServiceImpl implements ICacheService {

	// 调用成功标识
	private static final String IS_OK = Protocol.Keyword.OK.name();
	
	/**
	 *  Put OutputObject to Cache
	 * @throws Exception 
	 */
	public boolean put2Cache(String cacheKey, OutputObject outputObject) throws MyOwnRuntimeException {
		//JedisCluster cluster = JedisUtil.getCluster();
		//String result = cluster.set(cacheKey, JsonUtil.convertObject2Json(outputObject));
		Jedis jedis=JedisSingleUtil.getJedis();
		String result=jedis.set(cacheKey, JsonUtil.convertObject2Json(outputObject));
		return IS_OK.equals(result);
	}
	
	/**
	 *  Put OutputObject to Cache
	 * @throws Exception 
	 */
	public boolean put2Cache(String cacheKey, OutputObject outputObject, int expiry) throws MyOwnRuntimeException {
//		JedisCluster cluster = JedisUtil.getCluster();
//		String result = cluster.setex(cacheKey, expiry, JsonUtil.convertObject2Json(outputObject));
		Jedis jedis=JedisSingleUtil.getJedis();
		String result=jedis.setex(cacheKey, expiry,JsonUtil.convertObject2Json(outputObject));
		return IS_OK.equals(result);
	}

	/**
	 *  Get OutputObject from Cache
	 */
	public OutputObject getFromCache(String cacheKey) throws MyOwnRuntimeException {
		//JedisCluster cluster = JedisUtil.getCluster();
		Jedis jedis=JedisSingleUtil.getJedis();
		String result=jedis.get(cacheKey);
		if(StringUtil.isEmpty(result))
		{
			return null;
		}
		return JsonUtil.json2OutputObject(jedis.get(cacheKey));
	}

	/**
	 * Remove All Caches
	 */
	public void removeAll() {
	}
	
	/**
	 * 保存对象到缓存中
	 * 
	 * @param key
	 *            对象在缓存系统中的Key
	 * @param o
	 *            对象实例
	 * @param expiry
	 *            缓存过期时间(s) 3600s = 1h
	 * @param group
	 *            缓存区域
	 * @return 保存是否成功
	 */
	protected boolean put(String myKey, Object myValue, long expiry,
			String group) {
		return false;
	}

	/**
	 * 保存对象到缓存中
	 * 
	 * @param key
	 *            对象在缓存系统中的Key
	 * @param o
	 *            对象实例
	 * @param expiry
	 *            缓存过期时间(s) 3600s = 1h
	 * @return 保存是否成功
	 */
	protected boolean put(String key, Object o, long expiry) {
		return false;
	}

	/**
	 * 从缓存中删除对象
	 * 
	 * 注意！！！如果put时加了group参数，则remove时也要加group参数
	 * 
	 * @param key
	 *            对象在缓存系统中的Key
	 * @return 删除是否成功
	 */
    public boolean remove(String key) throws MyOwnRuntimeException {
        return false;
    }

	/**
	 * 从缓存中读取对象
	 * 
	 * 注意！！！如果put时加了group参数，则get时也要加group参数
	 * 
	 * @param key
	 *            对象在缓存系统中的Key
	 * @return 对象实例
	 */
	protected Object get(String key) {
		return null;
	}

	/**
	 * 从缓存中读取对象
	 * 
	 * @param key
	 *            对象在缓存系统中的Key
	 * @param group
	 *            缓存区域
	 * @return 对象实例
	 */
	protected Object get(String key, String group) {
		return null;
	}

	/**
	 * 从缓存中删除对象
	 * 
	 * @param key
	 *            对象在缓存系统中的Key
	 * @param group
	 *            缓存区域
	 * @return 删除是否成功
	 */
	protected boolean remove(String key, String group) {
		return false;
	}
	
	@Override
	public String incr(String key) throws MyOwnRuntimeException {
		//JedisCluster cluster = JedisUtil.getCluster();
		Jedis jedis=JedisSingleUtil.getJedis();
		return jedis.incr(key).toString();
	}
}
